#include "t0022-0.h"
#include "cui.h"

#define MODNAME     "[LAY]"
/*
    {
        "name": "menubar",
        "size": {
            "lines": 0.8,
            "cols": 0.2,
            "beginx": 0,
            "beginy": 2
        }
    },
*/

void cui_dump_layout(struct list_head *layoutgrouplist)
{
    struct cui_layoutgroup_st *layoutgroup;
    struct cui_layout_st *layout;
    ERRSYS_INFOPRINT("##### DUMP LAYOUT BEGIN ######\n");
    list_for_each_entry(layoutgroup, layoutgrouplist, node) {
        ERRSYS_INFOPRINT("Layout Group:[%s]\n", layoutgroup->name);
        list_for_each_entry(layout, &layoutgroup->layoutlist, node) {
            ERRSYS_INFOPRINT("%*s name: %s\n", 2, "", layout->name);
            ERRSYS_INFOPRINT("%*s x:%f y:%f rows:%f cols:%f\n", 2, "", layout->beginx, layout->beginy, layout->lines, layout->cols);
        }
    }
    ERRSYS_INFOPRINT("##### DUMP LAYOUT END ######\n");
}

struct cui_layout_st* cui_layout_parse(cJSON *root, struct cui_layoutgroup_st *layoutgroup)
{
    struct cui_layout_st *layout = zmalloc(sizeof(struct cui_layout_st));
    int retval = -1;
    ERRSYS_INFOPRINT("%s: \n", __func__);
    if (root != NULL && layoutgroup != NULL && layout != NULL) {
        cJSON *node, *size;
        // cui_dump_widget(widget);
        node = cJSON_GetObjectItem(root, CUI_CONFIELD_NAME);
        ERRSYS_INFOPRINT("%s: layout name node %p\n", __func__, node);
        if (cJSON_ValidString(node)) {
            strncpy(layout->name, node->valuestring, CUI_WIDGETTITLE_BUFLEN);

            size = cJSON_GetObjectItem(root, CUI_CONFIELD_SIZE);
            if (size != NULL) {
                node = cJSON_GetObjectItem(size, CUI_CONFIELD_BEGINX);
                if (cJSON_ValidNumber(node) && node->valueint != 0) {
                    layout->beginx = node->valueint;
                }
                else if (cJSON_ValidNumber(node) && node->valuedouble != 0) {
                    layout->beginx = node->valuedouble;
                    layout->flags |= LAYOUT_FLAGS_BEGINX_PERCENT;
                }

                node = cJSON_GetObjectItem(size, CUI_CONFIELD_BEGINY);
                if (cJSON_ValidNumber(node) && node->valueint != 0) {
                    layout->beginy = node->valueint;
                }
                else if (cJSON_ValidNumber(node) && node->valuedouble != 0) {
                    layout->beginy = node->valuedouble;
                    layout->flags |= LAYOUT_FLAGS_BEGINY_PERCENT;
                }

                node = cJSON_GetObjectItem(size, CUI_CONFIELD_LINES);
                if (cJSON_ValidNumber(node)) {
                    // ERRSYS_INFOPRINT("%s: lines %d %f\n", __func__, node->valueint, node->valuedouble);
                    layout->lines = node->valueint;
                    if (node->valuedouble != 0.0f) {
                        layout->lines = node->valuedouble;
                        layout->flags |= LAYOUT_FLAGS_LINES_PERCENT;
                    }
                }
                // if (cJSON_ValidNumber(node) && node->valueint != 0) {
                //     layout->lines = node->valueint;
                // }
                // else if (cJSON_ValidNumber(node) && node->valuedouble != 0) {
                //     layout->lines = node->valuedouble;
                //     layout->flags |= LAYOUT_FLAGS_LINES_PERCENT;
                // }
                node = cJSON_GetObjectItem(size, CUI_CONFIELD_COLS);
                if (cJSON_ValidNumber(node)) {
                    // ERRSYS_INFOPRINT("%s: cols %d %f\n", __func__, node->valueint, node->valuedouble);
                    layout->cols = node->valueint;
                    if (node->valuedouble != 0.0f) {
                        layout->cols = node->valuedouble;
                        layout->flags |= LAYOUT_FLAGS_COLS_PERCENT;
                    }
                }

                // node = cJSON_GetObjectItem(size, CUI_CONFIELD_COLS);
                // if (cJSON_ValidNumber(node) && node->valueint != 0) {
                //     layout->cols = node->valueint;
                // }
                // else if (cJSON_ValidNumber(node) && node->valuedouble != 0) {
                //     layout->cols = node->valuedouble;
                //     layout->flags |= LAYOUT_FLAGS_COLS_PERCENT;
                // }

            }
            retval = 0;
        }
    }

    if (retval == -1) {
        free(layout);
        layout = NULL;
    }
    return layout;
}
/*
{
    "name": "appdata"
    "layoutgroup" :[]
}
*/
struct cui_layoutgroup_st* cui_layoutgroup_parse(cJSON *root)
{
    int i, n;
    cJSON *node, *group;
    struct cui_layoutgroup_st *layoutgroup = zmalloc(sizeof(struct cui_layoutgroup_st));
    struct cui_layout_st *layout;
    int retval = -1;
    ERRSYS_INFOPRINT("%s: \n", __func__);
    if (layoutgroup != NULL) {
        INIT_LIST_HEAD(&layoutgroup->layoutlist);
        node = cJSON_GetObjectItem(root, CUI_CONFIELD_NAME);
        ERRSYS_INFOPRINT("%s: group name node %p\n", __func__, node);
        if (cJSON_ValidString(node)) {
            strncpy(layoutgroup->name, node->valuestring, CUI_WIDGETTITLE_BUFLEN);
            group = cJSON_GetObjectItem(root, CUI_CONFIELD_LAYOUTGROUP);
            ERRSYS_INFOPRINT("%s: layoutgroup node %p\n", __func__, group);
            if (cJSON_ValidArray(group)){
                n = cJSON_GetArraySize(group);
                for (i = 0;i < n;i ++) {
                    node = cJSON_GetArrayItem(group, i);
                    layout = cui_layout_parse(node, layoutgroup);
                    if (layout != NULL) {
                        list_add_tail(&layout->node, &layoutgroup->layoutlist);
                    }
                }
            }
        }

        retval = 0;
    }


    if (retval == -1) {
        if (layoutgroup) {
            struct list_head *pos;
            list_for_each(pos, &layoutgroup->layoutlist) {
                layout = list_entry(pos, struct cui_layout_st, node);
                free(layout);
            }
            free(layoutgroup);
        }
    }

    return layoutgroup;
}

/*
    "layoutgrouplist":[
        {
            "name": "appdata",
            "layoutgroup":[
                {
                    "name": "menubar",
                    "size": {
                        "lines": 0.8,
                        "cols": 0.2,
                        "beginx": 0,
                        "beginy": 2
                    }
                },
                {
                    "name": "datadesc",
                    "size": {
                        "lines": 0.2,
                        "cols": 0.2,
                        "beginx": 0,
                        "beginy": 0.8
                    }
                },
                {
                    "name": "dataview",
                    "size": {
                        "lines": 0.8,
                        "cols": 0.2,
                        "beginx": 0.2,
                        "beginy": 2
                    }
                }
            ]
        }
    ],
*/

// "layoutgrouplist":[] <- root = array
int cui_layoutgrouplist_parse(cJSON *root, struct cui_screen_st *cscr)
{
    int retval = -1;
    int n;
    cJSON *node, *node_layoutgrouplist;
    ERRSYS_INFOPRINT("%s: root %p cscr %p\n", __func__, root, cscr);
    if (root != NULL && cscr != NULL) {
        node_layoutgrouplist = cJSON_GetObjectItem(root, CUI_CONFIELD_LAYOUTGROUPLIST);
        ERRSYS_INFOPRINT("grouplist node %p\n", node_layoutgrouplist);
        if (cJSON_ValidArray(node_layoutgrouplist)) {
            n = cJSON_GetArraySize(node_layoutgrouplist);
            ERRSYS_INFOPRINT("grouplist node size %d\n", n);
            if (n != 0) {
                int i;
                for (i = 0;i < n;i ++) {
                    struct cui_layoutgroup_st *layoutgroup = NULL;
                    node = cJSON_GetArrayItem(node_layoutgrouplist, i);
                    layoutgroup = cui_layoutgroup_parse(node);
                    if (layoutgroup != NULL) {
                        list_add_tail(&layoutgroup->node, &cscr->layoutgrouplist);
                    }
                }
            }
            retval = 0;
        }
    }

    return retval;
}
